import { observable, action } from 'mobx';
import * as API from '../apis/schedulediffreport';
import * as Util from '../util/index';
import cloneDeep from 'lodash/cloneDeep'
import {Modal,message} from 'antd'
import {WeaTableNew,WeaForm} from 'comsMobx'
import { toJS } from 'mobx';
import isEmpty from 'lodash/isEmpty';
const {TableStore} = WeaTableNew;
const confirm = Modal.confirm;
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmScheduleDiffReport {
  title = () => getLabel('16559',"考勤报表");
  @observable dialogTitle = '考勤明细';
  @observable loading = false;
  @observable form = new WeaForm();
  @observable table = new TableStore();
  @observable config = [];
  @observable configbak = [];
  @observable configall = [];
  @observable reportdata = {};
  @observable visible = false;
  @observable lastname = "";
  @observable radiogroupdata = {};
  @observable fromDate = '';
  @observable toDate = '';
  @observable detialexcelurl = '';
  @observable selectedKey = '0';
  @observable topTab = [];

  @action
  getSearchCondition(params={}){
    this.config = [];
    this.configbak = [];
    this.configall = [];
    API.getSearchCondition(params).then(data=>{
      if (data.status == '1') {
        let c = cloneDeep(data.conditions);
        let tmpCondition = [];
        tmpCondition.push(c[0]);
        tmpCondition.push(c[1]);
        this.configbak = cloneDeep(tmpCondition);
        this.configall = data.conditions;
        this.config = cloneDeep(tmpCondition);
      } else {
          message.warning(data.message);
      }
    }, error=> {
        message.warning(error.message);
    })
  }

  @action
  setSelectedKey = (key)=>{
    this.selectedKey = key;
    this.getTableData();
  }

  updateVisible(v) {
    let params = this.radiogroupdata;
    let fromDate = this.fromDate || '';
    let toDate = this.toDate || '';
    let resourceid = params.resourceId || '';
    let departmentid = params.departmentId || '';
    let subcompanyid = params.subCompanyId || '';
    this.selectedKey = '0';

    if(fromDate!='' && toDate!=''){
      if(fromDate > toDate) {
        message.warning(getLabel('386537',"开始日期不能大于结束日期"));
        return;
      }
      if(params.viewscope=='1' && resourceid=="" && departmentid=="" && subcompanyid=="" ){
        message.warning(getLabel('386536',"请选择数据范围"));
        return;
      }
    }
    this.visible = v;
  }

  updateDialogTitle() {
    this.dialogTitle = "";
    if(this.stsType=='SIGININ'){
      this.dialogTitle = getLabel('20241',"签到明细");
      this.detialexcelurl = "HrmScheduleDiffSignInDetail&tnum=20241";
    }else if(this.stsType=='SIGINOUT'){
      this.dialogTitle = getLabel('20242',"签退明细");
      this.detialexcelurl = "HrmScheduleDiffSignOutDetail&tnum=20242";
    }else if(this.stsType=='BELATE'){
      this.dialogTitle = getLabel('20088',"迟到明细")
      ;
      this.detialexcelurl = "HrmScheduleDiffBeLateDetail&tnum=20088";
    }else if(this.stsType=='LEAVEEARLY'){
      this.dialogTitle = getLabel('20089',"早退明细");
      this.detialexcelurl = "HrmScheduleDiffLeaveEarlyDetail&tnum=20089";
    }else if(this.stsType=='ABSENT'){
      this.dialogTitle = getLabel('20090',"旷工明细");
      this.detialexcelurl = "HrmScheduleDiffAbsentFromWorkDetail&tnum=20090";
    }else if(this.stsType=='NOSIGN'){
      this.dialogTitle = getLabel('20091',"漏签明细");
      this.detialexcelurl = "HrmScheduleDiffNoSignDetail&tnum=20091";
    }else if(this.stsType=='LEAVE'){
      this.dialogTitle = getLabel('20092',"请假明细");
      this.detialexcelurl = "HrmScheduleDiffLeaveDetail&tnum=20092";
    }else if(this.stsType=='EVECTION'){
      this.dialogTitle = getLabel('20093',"出差明细");
      this.detialexcelurl = "HrmScheduleDiffEvectionDetail&tnum=20093";
    }else if(this.stsType=='OUTDAYS'){
      this.dialogTitle = getLabel('20094',"公出明细");
      this.detialexcelurl = "HrmScheduleDiffOutDetail&tnum=20094";
    }else if(this.stsType=='OVERTIME'){
      this.dialogTitle = getLabel('33501',"加班明细");
      this.detialexcelurl = "HrmScheduleOvertimeWorkDetail&tnum=33501";
    }else if(this.stsType=='OTHER'){
      this.dialogTitle = getLabel('386409',"考勤异常数据抵冲明细");
      this.detialexcelurl = "HrmScheduleDiffOtherDetail&tnum=127655,17463";
    }
  }

  getTableData() {
    let params = this.radiogroupdata;
    params.kqtype = this.stsType;
    params.from = 'schedulediffreport';
    params.selectedKey = this.selectedKey;
    this.updateDialogTitle();
    API.getTableData(params).then((datas)=> {
      this.table.getDatas(datas.sessionkey,1);
    })
  }

  getHrmKQReportDetialTabInfo(){
    let params = this.radiogroupdata;
    params.kqtype = this.stsType;
    API.getHrmKQReportDetialTabInfo(params).then((datas)=> {
      this.topTab = cloneDeep(datas.tabs);
      this.selectedKey = '0';
    })
  }

  getTabTableData(){
		API.getTabTableData().then(rs => {
			rs.map((result, index) => {
        switch (index) {
          case 0:
            this.getHrmKQReportDetialTabInfo();
            break;
          case 1:
            this.getTableData();
            break;
        }
			})
		})
  }
  
  exportExcel(){
    let params = this.radiogroupdata;
    let fromDate = this.fromDate || '';
    let toDate = this.toDate || '';
    let resourceid = params.resourceId || '';
    let departmentid = params.departmentId || '';
    let subcompanyid = params.subCompanyId || '';

    if(fromDate == '' || toDate == ''){
      message.error(getLabel('383565',"请选择日期范围"));
      return;
    }

    if(fromDate!='' && toDate!=''){
      if(fromDate > toDate) {
        message.warning(getLabel('386537',"开始日期不能大于结束日期"));
        return;
      }
      if(params.viewscope=='1' && resourceid=="" && departmentid=="" && subcompanyid=="" ){
        message.warning(getLabel('386536',"请选择数据范围"));
        return;
      }
    }
    window.location.href = "/hrm/report/schedulediff/HrmScheduleDiffReportExcelE9.jsp?cmd=HrmScheduleDiffReport&tnum=20078"+
                         "&fromDate="+this.fromDate+"&toDate="+this.toDate+"&resourceId="+resourceid+"&departmentId="+departmentid+
                         "&subCompanyId="+subcompanyid+"&viewscope="+params.viewscope;
  }

  exportDetialExcel(){
    let params = this.radiogroupdata;
    let fromDate = this.fromDate || '';
    let toDate = this.toDate || '';
    let resourceid = params.resourceId || '';
    let departmentid = params.departmentId || '';
    let subcompanyid = params.subCompanyId || '';
    let url = "/hrm/report/schedulediff/HrmScheduleDiffReportExcelE9.jsp?cmd="+this.detialexcelurl+"&fromDate="+this.fromDate+"&toDate="+this.toDate+"&resourceId="+resourceid+"&departmentId="+departmentid+
              "&subCompanyId="+subcompanyid+"&viewscope="+params.viewscope+"&selectedKey="+this.selectedKey;
    window.location.href = url;
  }

  getReportData(params={}){
    
    if(params.viewscope=='1'){
      this.config = this.configall;
      let resourceid = params.resourceId || '';
      let departmentid = params.departmentId || '';
      let subcompanyid = params.subCompanyId || '';
      if(resourceid=='' && departmentid=='' && subcompanyid==''){
        //message.warning("请选择数据范围");
        return false;
      }
    }else{
      this.config = this.configbak;
      params.resourceId = '';
      params.departmentId = '';
      params.subCompanyId = '';
    }
    
    this.radiogroupdata = params;
    
    this.loading=true;
    API.getReportData(params).then(data=>{
      if (data.status == '1') {
        this.reportdata = data.reportdata;
        this.fromDate = data.fromDate;
        this.toDate = data.toDate;
        this.loading=false;
      } else {
          message.warning(data.message);
      }
    }, error=> {
        message.warning(error.message);
    })
  }
}